
#import "AttributesInspector.h"
#import "Tree.h"

@implementation AttributesInspector

- init
{  
   	[super init];
	view = nil;	
	return self;
}
   
- (BOOL)wantsButtons 
{ 
	return NO; 
}

- (BOOL)doesHandleSelection
{
	char buf[MAXPATHLEN + 1];
    id bundle;
	
	if([self selectionCount] == 1 && [[self selection] isMemberOf:[Tree class]]){
		if(!view){
		 	bundle = [NXBundle mainBundle];
    		[bundle getPath:buf forResource:"AttributesInspector" ofType:"nib"];
   			[NXApp loadNibFile:buf owner:self withNames:NO fromZone:[self zone]];
		}
		return YES;
	}
	return NO;
}

- ok:sender
{
	return [super ok:sender];
}

- revert:sender
{
	[super revert:sender];
	[clearAttachment setEnabled:[[self selection] attached]];
	[clearSound setEnabled:[[self selection] hasSound]];
	[soundView setSound:[[self selection] attachedSound]];
	return self;
}
  
- changeAttachment:sender
{
	const char *localDirectory;
	const char *const *localFiles;
	char buffer[MAXPATHLEN];
	id openpanel = [OpenPanel new];
	const char *const *fileTypes = {NULL};
	
	if([openpanel runModalForTypes:fileTypes]){
		localDirectory = [openpanel directory];
		localFiles = [openpanel filenames];
		strcpy(buffer,localDirectory);
		strcat(buffer,"/");
		strcat(buffer,*localFiles);
		[clearAttachment setEnabled:YES];
		[[self selection] attachFile:buffer];
	}
	return self;
}

- clearAttachment:sender
{
	[[self selection] breakAttachment];
	[clearAttachment setEnabled:NO];
	return self;
}

- changeSound:sender
{
	const char *localDirectory;
	const char *const *localFiles;
	char buffer[MAXPATHLEN];
	id openpanel = [OpenPanel new];
	const char *const fileTypes[2] = {"snd",NULL};
	
	if([openpanel runModalForTypes:fileTypes]){
		localDirectory = [openpanel directory];
		localFiles = [openpanel filenames];
		strcpy(buffer,localDirectory);
		strcat(buffer,"/");
		strcat(buffer,*localFiles);
		[clearSound setEnabled:YES];
		[[self selection] attachSound:buffer];
		[soundView setSound:[[self selection] attachedSound]];
	}
	return self;
}

- clearSound:sender
{
	[[self selection] breakSound];
	[clearSound setEnabled:NO];
	return self;
}

- erase:sender
{	
	return self;
}

- pause:sender
{
	if([pauseButton state]){
		[playButton setEnabled:NO];
		[recordButton setEnabled:NO];
		[stopButton setEnabled:NO];
		[soundView pause:self];
	} else {
		[playButton setEnabled:YES];
		[recordButton setEnabled:YES];
		[stopButton setEnabled:YES];
		[soundView resume:self];
	}
	return self;
}

- play:sender
{
	[playButton setEnabled:NO];
	[recordButton setEnabled:NO];
	[stopButton setEnabled:YES];
	[pauseButton setState:0];
	[soundView play:self];
	return self;
}

- record:sender
{	
	[recordButton setEnabled:NO];
	[playButton setEnabled:NO];
	[stopButton setEnabled:YES];
	[soundView record:self];
	return self;
}

- stop:sender
{
	[soundView stop:self];
	return self;
}

- scale:sender
{
	[soundView setReductionFactor:[sender floatValue]];
	return self;
}

// SoundView delegate methods

- didPlay:sender
{
	[soundMeter stop:self];
	[playButton setState:0];
    [playButton setEnabled:YES];
    [recordButton setState:0];
    [recordButton setEnabled:YES];
    [pauseButton setState:0];
	return self;
}

- willPlay:sender
{
	[soundMeter setSound:[sender soundBeingProcessed]];
	[soundMeter run:self];
	return self;
}

- willRecord:sender
{
	[soundMeter setSound:[sender soundBeingProcessed]];
	[soundMeter run:self];
	return self;
}

- didRecord:sender
{
	[soundMeter stop:self];
	[playButton setState:0];
    [playButton setEnabled:YES];
    [recordButton setState:0];
    [recordButton setEnabled:YES];
    [pauseButton setState:0];
	if(![[self selection] hasSound]){
		[[self selection] setSound:[soundView sound]];
		[clearSound setEnabled:YES];
	}
	return self;
}

- soundDidChange:sender
{
	if(![[self selection] hasSound]){
		[[self selection] setSound:[soundView sound]];
		[clearSound setEnabled:YES];
	}
	return self;
}

- hadError:sender
{
    int err = [[sender soundBeingProcessed] processingError];

    if([playButton state])
	NXRunAlertPanel("Play error",
	    SNDSoundError(err),
	    "OK",
	    NULL,NULL);
    else if([recordButton state])
	NXRunAlertPanel("Record error",
	    SNDSoundError(err),
	    "OK",
	    NULL,NULL);
    return [self stop:self];
}

@end
